var $SohuNewsScope = window.$SohuNewsScope || {};
$SohuNewsScope.post = $SohuNewsScope.post || (function() {


    function loadXMLDoc(url, data, onSuccess, onError, onComplete, headerObj) {
        var xhr = null;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xhr !== null) {
            xhr.onreadystatechange = function() {
                state_Change(xhr, onSuccess, onError, onComplete);
            };
            xhr.open("POST", url, true);

            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            if (!!headerObj) {
                var headerKey;
                var headerValue;
                for (headerKey in headerObj) {
                    headerValue = headerObj[headerKey];
                    xhr.setRequestHeader(headerKey, headerValue);
                }
            }
            // xhr.setRequestHeader("S_COOKIE", '78c76cf58a0619f04e490d3fcd42289d06252203a591983294788513db9cd2bf7013a1a7e0bc2e2ba233d2936f7f4584');

            // xhr.send(data);


            // 只要是在trackerData中得数据，统统打给日志系统
            var paramArr = [],
                key;
            for (key in data) {
                if (data.hasOwnProperty(key)) {
                    paramArr.push(key + '=' + encodeURIComponent(data[key]));
                }
            }

            xhr.send(paramArr.join('&'));
        } else {
            if (!!onError && typeof onError === 'function') {
                onError();
            }
            if (!!onComplete && typeof onComplete === 'function') {
                onComplete();
            }
            throw ("Your browser does not support XMLHTTP.");
        }
    }

    function state_Change(xhr, onSuccess, onError, onComplete) {
        if (xhr.readyState == 4) {
            if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
                if (!!onSuccess && typeof onSuccess === 'function') {
                    var responseText;
                    try {
                        responseText = JSON.parse(xhr.responseText);
                    } catch (e) {
                        responseText = xhr.responseText;
                    }
                    onSuccess(responseText);
                }
                if (!!onComplete && typeof onComplete === 'function') {
                    onComplete();
                }
            } else {
                if (!!onError && typeof onError === 'function') {
                    onError(xhr);
                }
                if (!!onComplete && typeof onComplete === 'function') {
                    onComplete();
                }
                throw ("Problem retrieving XML data");
            }
        }
    }

    return loadXMLDoc;
}());
// $$.get('/example/data/news-list1.json', function() {
//     alert('success');
// }, function() {
//     alert('error');
// }, function() {
//     alert('complete');
// });